/**
 * Copyright (C) 2015 coderyuan.com. All Rights Reserved.
 *
 * CoderyuanApiLib
 *
 * Requester.java created on 2015年7月9日
 *
 * @author yuanguozheng
 * @version v1.0.0
 * @since 2015年7月9日
 */
package com.coderyuan.http;

import java.util.HashMap;
import java.util.TreeMap;

import com.coderyuan.crypto.DesUtil;
import com.coderyuan.http.HttpUtil.Method;
import com.coderyuan.utils.TokenUtil;

import android.text.TextUtils;

/**
 * @author yuanguozheng
 */
public class Requester {

    private static HttpUtil sGetUtil;
    private static HttpUtil sPostUtil;
    private static HttpUtil sPostFileUtil;

    public static String doGet(String uri) {
        if (sGetUtil == null) {
            sGetUtil = new HttpUtil(Method.GET);
        }
        sGetUtil.setUrl(uri);
        HttpResponseModel response = sGetUtil.request();
        if (!TextUtils.isEmpty(response.getStringData())) {
            return response.getStringData();
        }
        return null;
    }

    public static String doPost(String uri, HashMap<String, String> params) {
        if (sPostUtil == null) {
            sPostUtil = new HttpUtil(Method.POST);
        }
        sPostUtil.clear();
        sPostUtil.setUrl(uri);
        sPostUtil.setRequestStringParams(params);
        HttpResponseModel response = sPostUtil.request();
        if (!TextUtils.isEmpty(response.getStringData())) {
            return response.getStringData();
        }
        return null;
    }

    public static String doPost(String uri, TreeMap<String, String> params) {
        if (sPostUtil == null) {
            sPostUtil = new HttpUtil(Method.POST);
        }
        sPostUtil.clear();
        sPostUtil.setUrl(uri);
        for (String key : params.keySet()) {
            sPostUtil.addParam(key, params.get(key));
        }
        HttpResponseModel response = sPostUtil.request();
        if (!TextUtils.isEmpty(response.getStringData())) {
            return response.getStringData();
        }
        return null;
    }

    public static HttpResponseModel requestWithModel(HttpRequestModel reqModel) {
        if (sGetUtil == null) {
            sGetUtil = new HttpUtil(Method.GET);
        }
        if (sPostUtil == null) {
            sPostUtil = new HttpUtil(Method.POST);
        }
        if (sPostFileUtil == null) {
            sPostFileUtil = new HttpUtil(Method.POST_FILE);
        }
        HttpUtil util;
        switch (reqModel.getMethod()) {
            case GET:
                util = sGetUtil;
                break;
            case POST:
                util = sPostUtil;
                break;
            case POST_FILE:
                util = sPostFileUtil;
                break;
            default:
                util = sGetUtil;
                break;
        }
        util.clear();
        util.setUrl(reqModel.getUrl());
        util.setHeaders(reqModel.getHeaders());
        util.setRequestStringParams(reqModel.getStringParams());
        util.setNeedUrlencoded(reqModel.getNeedUrlEncoded());
        util.setFiles(reqModel.getFiles());
        return util.request();
    }

    public static HttpResponseModel postWithRawUrlParams(String url, String params) {
        if (sPostUtil == null) {
            sPostUtil = new HttpUtil(Method.POST);
        }
        sPostUtil.clear();
        sPostUtil.setUrl(url);
        sPostUtil.setDirectWriteParams(params);
        return sPostUtil.request();
    }

    public static String doPostWithToken(String uri, HashMap<String, String> params, String key) {
        String token = TokenUtil.getToken(key, params);
        params.put("token", token);
        return doPost(uri, params);
    }

    public static String doPostWithTokenByDes(String uri, HashMap<String, String> params, String key) {
        String token = TokenUtil.getToken(key, params);
        for (String paramKey : params.keySet()) {
            if (paramKey.equals("timestamp")) {
                continue;
            }
            String sValue = DesUtil.encode(key, params.get(paramKey));
            params.put(paramKey, sValue);
        }
        params.put("token", token);
        return doPost(uri, params);
    }
}
